package config

import (
	"log"
	"software/model"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

var DB *gorm.DB

func InitDB() {
	log.Println("开始初始化数据库...")

	var err error
	DB, err = gorm.Open(sqlite.Open("software.db"), &gorm.Config{})
	if err != nil {
		log.Fatal("Failed to connect to database:", err)
	}
	log.Println("数据库连接成功")

	// 自动迁移数据库表
	log.Println("开始迁移数据库表...")
	err = DB.AutoMigrate(&model.User{}, &model.VerificationCode{}, &model.Resume{})
	if err != nil {
		log.Fatal("Failed to migrate database:", err)
	}
	log.Println("数据库表迁移成功")

	log.Println("Database connected and migrated successfully")
}
